var rootPath = getRootPath();

$(function() {
	var dr = $("#dr").val();
	$.ajax({
		url : rootPath + '/school/selectPageSchool',
		type : 'POST',
		contentType : 'application/json',
		data : JSON.stringify({
			"dr" : dr
		}),
		success : function(data) {
			fillSchool(data);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});

	$("#sname").keyup(function(event) {
		if(event.keyCode == 13) {
			$("#searchSchool").click();
		}
		return false;
	});
	$("#searchSchool").click(function() {
		var dr = $("#dr").val();
		var sname = $("#sname").val();
		$.ajax({
			url : rootPath + '/school/selectPageSchool',
			type : 'POST',
			contentType : 'application/json',
			data : JSON.stringify({
				"dr" : dr,
				"sname" : sname
			}),
			success : function(data) {
				fillSchool(data);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				layer.alert("error:(");
			}
		});
		return false;
	});
});

function fillSchool(data) {
	var uname = $("#sname").val();
	if (data.returnCode == "0000") {
		console.log(data);
		var length = data.returnObject.list.length;
		$("#dataNum").html(data.returnObject.total);
		var tr;
		for (var i = 0; i < length; i++) {
			tr += "<tr class='text-c' name='schoolnameTr'>";
			tr += "<td>" + data.returnObject.list[i].sid + "</td>";
			tr += "<td>" + (data.returnObject.list[i].sname == undefined ? "" : data.returnObject.list[i].sname) + "</td>";
			tr += "<td>" + formatUndefined(data.returnObject.list[i].phone) + "</td>";
			tr += "<td>" + formatUndefined(data.returnObject.list[i].email) + "</td>";
			tr += "<td>" + formatUndefined(data.returnObject.list[i].uid) + "</td></tr>";
		
//			tr += "<td><a title='重置梦想主任' href='javascript:;' onclick='resetDirector(this,"
//					+ "\""
//					+ data.returnObject.list[i].sid
//					+ "\","
//					+ "\""
//					+ (data.returnObject.list[i].uid == undefined ? '' : data.returnObject.list[i].uid)
//					+ "\")' class='ml-5' style='text-decoration:none'><i class='Hui-iconfont'>&#xe63f;</i></a></td></tr>"
		}

		$("#schools").empty();
		$("#schools").append(tr);

		var page = "";
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ 1 + ")'>首页</span>";
		if (data.returnObject.hasPreviousPage) {
			page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.prePage + ")'>上一页</span>";
		}
		var num = data.returnObject.navigatepageNums.length;
		for (var i = 0; i < num; i++) {
			if (data.returnObject.navigatepageNums[i] == data.returnObject.pageNum) {
				page += "<span style='font-weight:900;padding:7px 10px;'>"
						+ data.returnObject.pageNum + "</span>";
			} else {
				page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
						+ data.returnObject.navigatepageNums[i]
						+ ")'>"
						+ data.returnObject.navigatepageNums[i] + "</span>";
			}
		}
		if (data.returnObject.hasNextPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.nextPage + ")'>下一页</span>";
		}
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ data.returnObject.pages + ")'>末页</span>";

		$("#page").empty();
		$("#page").append(page);
		
		$(".pageNavigate").mouseover(function() {
			$(this).css("cursor", "pointer");
		});
	} else {
		$("#dataNum").html("0");
	}
}

/**
 * 重置梦想主任
 */
function resetDirector(obj,sid,uid) {
	if(uid == undefined || uid == '') {
		layer.alert("贵校暂无梦想主任,无需重置:(");
		return;
	}
	
	layer.confirm('是否重置该梦想中心主任',function(index) {
		$.ajax({
			url : rootPath + '/school/resetDirector',
			type : 'GET',
			data : {
				"sid" : sid,
				"uid" : uid
			},
			success : function(data) {
				if ("0000" == data.returnCode) {
					layer.msg('梦想主任已重置', {
						icon : 1,
						time : 1000
					}, function() {
						window.location.href = rootPath + '/schoolManagerPage';
					});
				} else {
					layer.alert(data.returnMessage);
				}
			},
			error : function(jqXHR, textStatus, errorThrown) {
				layer.alert("重置梦想中心主任失败:(");
			}
		});
	});
}

function switchPage(pageNo) {
	$(this).css("cursor", "pointer");
	var dr = $("#dr").val();
	var sname = $("#sname").val();
	$.ajax({
		url : rootPath + '/school/selectPageSchool',
		type : 'POST',
		contentType : 'application/json',
		data : JSON.stringify({
			"dr" : dr,
			"sname" : sname,
			"pageNo" : pageNo
		}),
		success : function(data) {
			fillSchool(data);
			$(".pageNavigate").hover(function() {
				$(this).css("cursor", "pointer");
			});
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});
}

function formatUndefined(value) {
	if(value == undefined) {
		value = "";
	}
	return value;
}